<?php
namespace app\index\controller;

use think\Request;
use think\Controller;
use think\Session;
use think\Cookie;

//小店
class Retail extends xiaodian{

	//代理的零售页面
    public function index(){
		//return view();
    }
	
	//零售小店个人中心
	public function mine(){
		$param = ['agentid' => SHOP_ID,'customerid'=>CUST_ID];
		$response = \ServerHelper::post('retail_mine',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			$this->error($data['msg']);
		}
		return view('',$data['data']);
	}
	
	//零售小店个人信息
	public function member_info(){
		$param = ['agentid' => SHOP_ID,'customerid'=>CUST_ID];
		$response = \ServerHelper::post('retail_mine',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			$this->error($data['msg']);
		}
		return view('',$data['data']);
	}
	
	//执行更换自己的头像
	public function changeheadimage(){
		
		if(!isset($_FILES["image"]["error"])) ajax('请选择图片');
		if($_FILES["image"]["error"] == 1){
			ajax('图片尺寸过大');
		}else if($_FILES["image"]["error"] != 0){
			ajax('图片上传失败');
		}
		$extention = explode('.',$_FILES['image']['name']);
		$extention = end($extention);
		$allow = ['jpg','jpeg','pjpeg','gif','png'];
		if(!in_array(strtolower($extention),$allow)){
			ajax('文件格式不正确');
		}
		$ali = controller('Aliyun');
		$upinfo = $ali->frontupload();
		if(empty($upinfo['flag'])){
			ajax('头像上传失败');
		}
		$param = [
			'customerid' => CUST_ID,
			'action' => 'image',
			'value' => $upinfo['data']
		];
		$response = \ServerHelper::post('retail_customerinfo_save',$param);
		$data = json_decode($response,true);
		if(empty($data['flag'])){
			//优化：删除已上传的OSS图片
			ajax('头像保存失败');
		}else{
			ajax('头像上传成功',true,$upinfo['data']);
		}
	}
	
	//零售小店客户绑定手机号
	public function bindmobile(){
		$request = Request::instance();
		if($request->isPost()){
			$smscode = $request->param('smscode');
			//校验短信码
			$smscode_real = Cookie::get('smscode_resetphonenum');
			if($smscode == '' || $smscode != $smscode_real){
				ajax('短信码错误');
			}
			$param = [
				'agentid' => SHOP_ID,
				'customerid' => CUST_ID,
				'password' => $request->param('password'),//旧密码
				'phonenum' => $request->param('phonenum'),//新手机号
			];
			$response = \ServerHelper::post('retail_bindphonenum',$param);
			$data = $this->J($response);
			if(!isset($data['flag'])){
				ajax('修改失败');
			}
			if($data['flag'] == '0'){
				ajax($data['msg']);
			}
			Cookie::delete('smscode_resetphonenum');
			ajax('修改成功',true);
		}else{
			$param = ['agentid' => SHOP_ID,'customerid'=>CUST_ID];
			$response = \ServerHelper::post('retail_mine',$param);
			$data = $this->J($response);
			if(empty($data['flag'])){
				abort(404,'数据错误');
				exit();
			}
			return view('changephonenum',$data['data']);
		}
	}
	
	//零售客户个人订单
	public function orders(){
		$status = Request::instance()->param('status');
		if(!\RegexHelper::is_ids($status)){
			$status = -1;
		}
		$key = Request::instance()->param('key','');
		$page = Request::instance()->param('page',1,'intval');
		$param = [
			'agentid' => SHOP_ID,
			'customerid' => CUST_ID,
			'status' => $status,
			'key' => $key,
			'page' => $page,
		];
		$response = \ServerHelper::post('shop_order_list',$param);
		$data = $this->J($response);
		return view('',$data['data']);
	}
	
	//个人订单列表
	public function order_loadpage(){
		$status = Request::instance()->param('status');
		if(!\RegexHelper::is_ids($status)){
			$status = -1;
		}
		$key = Request::instance()->param('key','');
		$page = Request::instance()->param('page',1,'intval');
		$param = [
			'agentid' => SHOP_ID,
			'customerid' => CUST_ID,
			'status' => $status,
			'key' => $key,
			'page' => $page,
		];
		$response = \ServerHelper::post('shop_order_list',$param);
		$data = $this->J($response);
		$info = $data['data'];
		$list = [];
		foreach($info['orderinfo'] as $k=>$v){
			$one = $v;
			$one['Status'] = __order_status($v['Status']);
			$one['buttons'] = __order_status_btns_2($v['Status']);
			$list[] = $one;
		}
		$data = [
			'list' => $list,
			'status' => $info['search']['status'],
			'key' => $info['search']['key'],
			'page' => $info['search']['page'],
		];
		ajax('success',true,$data);
	}
	
	//零售客户 个人订单详情
	public function order_detail(){
		$orderid = Request::instance()->param('id');
		$param = [
			'customerid' => CUST_ID,
			'orderid' => $orderid
		];
		$response = \ServerHelper::post('shop_order_detail',$param);
		$data = $this->J($response);
		return view('',$data['data']);
	}
	
	//零售客户、自己对订单做出动作
	public function order_action(){
		$request = Request::instance();
		$act = $request->param('act');
		$orderid = $request->param('orderid');
		if($act == 'quxiao'){
			//取消订单
			$param = [
				'orderid' => $orderid,
				'customerid' => CUST_ID,
			];
			$response = \ServerHelper::post('shop_order_cancel',$param);
			$data = $this->J($response);
			ajax($data['msg'],$data['flag'] == 1);
		}else if($act == 'shouhuo'){
			//零售客户确认自己的订单收货
			$param = [
				'orderid' => $orderid,
				'customerid' => CUST_ID,
			];
			$response = \ServerHelper::post('shop_order_receipt',$param);
			$data = $this->J($response);
			ajax($data['msg'],$data['flag'] == 1);
		}
	}
	
	//零售客户查看自己订单的打款凭证
	public function order_checkrep(){
		$request = Request::instance();
		$param = [
			'orderid' => $request->param('orderid'),
			'customerid' => CUST_ID,
		];
		$response = \ServerHelper::post('shop_order_checkrep',$param);
		$data = $this->J($response);
		return view('',$data['data']);
	}
	//查看物流信息
	public function order_logistics(){
		$request = Request::instance();
		$param = [
			'orderid' => $request->param('orderid'),
			'customerid' => CUST_ID,
		];
		$response = \ServerHelper::post('shop_order_wuliu',$param);
		$data = $this->J($response);
		if(!isset($data['msg'])){
			$this->error('数据错误');
		}else if($data['flag'] == 0){
			$this->success($data['msg']);
		}else{
			return view('',$data);
		}
	}

	
	public function test(){
		
		$param = [
			'agentid' => SHOP_ID,
			'customerid' => CUST_ID,
		];
		$response = \ServerHelper::post('testtest',$param);
		var_dump($response);
		return view();
	}

}
